package br.ufrgs.inf.cwsmart.util;

import java.io.PrintStream;

public class Logger 
{
    public static final int NONE_LEVEL = 0;
    public static final int INFORMATIONAL_LEVEL = 1;
    public static final int WARNING_LEVEL = 2;
    public static final int DEBUG1_LEVEL = 3;
    public static final int DEBUG2_LEVEL = 4;
    public static final int DEBUG3_LEVEL = 5;
    public static final int DEBUG4_LEVEL = 6;
    public static final int DEBUG5_LEVEL = 7;
    
    public static final int MIN_DEBUG_LEVEL = DEBUG1_LEVEL;
    public static final int MAX_DEBUG_LEVEL = DEBUG5_LEVEL;

    public static Logger logger = new Logger(true);

    protected static String[] msg = {"none", "inform", "warning", "debug(1)",
                                     "debug(2)", "debug(3)", "debug(4)",
                                     "debug(5)"};

    protected int logLevel = NONE_LEVEL;
    
    protected int typeLength;

    protected PrintStream out = System.err;

    public Logger(boolean makeDefault, int typeLength) 
    {
        if (makeDefault)
            logger = this;

        this.typeLength = typeLength;
    }

    public Logger(boolean makeDefault) 
    {
        this(makeDefault, 24);
    }

    public Logger()
    {
        this(false, 24);
    }

    public int getLogLevel()
    {
        return logLevel;
    }

    public PrintStream getOut()
    {
        return out;
    }

    public void setLogLevel(int logLevel)
    {
        if (logLevel >= NONE_LEVEL && logLevel <= MAX_DEBUG_LEVEL)
            this.logLevel = logLevel;
    }

    public void setOut(PrintStream out)
    {
        if (out != null)
            this.out = out;
    }

    public void log(int level, String type, String message)
    {        
    	
        if (level > NONE_LEVEL && level <= MAX_DEBUG_LEVEL
            && level <= logLevel) {

        	int wLength = typeLength - type.length();
            StringBuffer white = new StringBuffer();
            for (int i = 0; i < wLength; i++)
                white.append(' ');

            out.println(msg[level] + " [" + type + "]:" + white + message);
            
        }
    }
}